package com.lzxuni.modules.basics.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.GetPinyin;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.basics.entity.BasicsLand;
import com.lzxuni.modules.basics.service.BasicsLandService;
import com.lzxuni.modules.basics.service.IdgeneratorService;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 基础信息_地块管理 前端控制器
 * </p>
 *
 * @author LaoNa
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/Basics/Land")
public class BasicsLandController extends BaseController {

    @Autowired
    private BasicsLandService basicsLandService;
    @Autowired
    private IdgeneratorService idgeneratorService;
    private String templatesPath = "/Basics/Land";

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView(templatesPath + "/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination, String queryJson) {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        BasicsLand basicsLand = JSON.parseObject(queryJson, BasicsLand.class);
        PageData pageData = getPageData(basicsLandService.queryPage(pageParameter, basicsLand));
        return R.ok().put("data", pageData);
    }

    //表单页
    @RequestMapping("/Form")
    public ModelAndView Form(BasicsLand basicsLand) {
        ModelAndView mv = new ModelAndView(templatesPath + "/form");
        mv.addObject("basicsLand", basicsLand);
        return mv;
    }

    //删除数据
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        basicsLandService.removeById(keyValue);
        basicsLandService.remove(new QueryWrapper<BasicsLand>().eq("parent_id", keyValue));
        return R.ok("删除成功");
    }

    //保存数据
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue, BasicsLand basicsLand) {
        basicsLand.setLandId(keyValue);
        if (StringUtils.isNotBlank(basicsLand.getLandAdministration())) {
            String temp[] = basicsLand.getLandAdministration().split("-");
            basicsLand.setParentId(temp[0]);
            basicsLand.setLandNum(temp[1] + "" + idgeneratorService.getThreeSequence("子地块序号"));
            basicsLand.setLandAdministration(temp[1]);
        } else {
            basicsLand.setLandNum(GetPinyin.getPinYinHead(basicsLand.getAreaName()) + idgeneratorService.getThreeSequence("地块序号"));
            basicsLand.setParentId("-1");
            basicsLand.setLandAdministration("");
        }
        if (StringUtils.isEmpty(basicsLand.getLandId())) {
            basicsLandService.save(basicsLand);
            return R.ok("保存成功");
        } else {
            int temp_lent = basicsLandService.list(new QueryWrapper<BasicsLand>().eq("parent_id", basicsLand.getLandId())).size();
            if (temp_lent < 1) {
                basicsLandService.updateById(basicsLand);
                return R.ok("修改成功");
            } else {
                return R.error(401, "该地块有二级地块禁止修改");
            }

        }
    }


    @RequestMapping("/GetParentTree")
    public Object getParentTree(String landId, String parentId) throws Exception {

        return R.ok().put("data", basicsLandService.getParentTree(landId, parentId));
    }


}

